package com.bookstore.admin.notice.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bookstore.admin.notice.service.IAdminNoticeService;
import com.bookstore.commons.beans.Notice;
import com.bookstore.utils.PageModel;

@Controller
@RequestMapping("/admin/notice")

public class AdminNoticeHandlers {

	@Autowired
	private IAdminNoticeService adminNoticeService;
	
	@RequestMapping("/findNotices.do")
	public String findNotices(Model model,@RequestParam(defaultValue="1")Integer pageIndex){
		
		PageModel pageModel = new PageModel();
		pageModel.setPageIndex(pageIndex);
		
		int count = adminNoticeService.findNoticeCount(pageIndex);
		pageModel.setRecordCount(count);
		List<Notice> notices = adminNoticeService.findNotice(pageModel);
		
		System.out.println(notices);
		model.addAttribute("pageModel", pageModel);
		model.addAttribute("notices", notices);
		return "/admin/notices/list.jsp";
	}
	
	@RequestMapping("/removeNotice.do")
	public String removeNotice(Integer id){
		
		int rows = adminNoticeService.removeNotice(id);
		if(rows > 0){
			return "redirect:/admin/notice/findNotices.do";
		}
		return "";
		
	}
	
	@RequestMapping("/findNoticeById.do")
	public String initModify(Integer id,Model model){
		
		Notice notice = adminNoticeService.findNoticeById(id);
		model.addAttribute("notice", notice);
		return "/admin/notices/edit.jsp";
	}
	
	@RequestMapping("/modifyNotice.do")
	public String modifyNotice(Notice notice){
		
		adminNoticeService.modifyNotice(notice);
		return "redirect:/admin/notice/findNotices.do";
	}
	
	@RequestMapping("/addNotice.do")
	public String addNotice(Notice notice){
		adminNoticeService.addNotice(notice);
		
		return "redirect:/admin/notice/findNotices.do";
	}
	
}
